import { fileURLToPath, URL } from 'node:url'

import { type ConfigEnv, loadEnv, type UserConfigExport } from 'vite'

import { createViteConfig } from './build/vite/conf'
import { createVitePlugins } from './build/vite/plugins'

// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  const root = process.cwd()
  const env = loadEnv(mode, root) as ImportMetaEnv
  const { VITE_PORT } = env
  return {
    ...createViteConfig(env),
    server: {
      port: Number(VITE_PORT)
    },
    plugins: createVitePlugins(env, command),
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    }
  }
}
